﻿using UnityEngine;
using UnityEditor;
using System.IO;
using System.Xml;
using System.Collections;
using System.Collections.Generic;
using System.Security.Cryptography;
using GX;

public class CreateMD5List
{
    const string ResourceFileName = "Resources";
    public static void Execute(UnityEditor.BuildTarget target)
    {
        string platform = AssetBundleController.GetPlatformName(target);
        Execute(platform);
        AssetDatabase.Refresh();
    }

    public static void Execute(string platform)
    {
        Dictionary<string, string> DicFileMD5 = new Dictionary<string, string>();
        
        string dir = System.IO.Path.Combine(Application.dataPath, "AssetBundlePackage/AssetBundle/" + platform); //路径整合
        foreach (string filePath in Directory.GetFiles(dir))
        {
            if (filePath.Contains(".meta") || filePath.Contains("VersionMD5") || filePath.Contains(".xml"))
                continue;

            //FileStream file = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            string strMD5 = GX.MD5.ComputeHashStringFromFile(filePath);
            //file.Close();
            string key = filePath.Substring(filePath.IndexOf(ResourceFileName) + ResourceFileName.Length + 1);
            key = key.Replace('\\', '/');

            if (DicFileMD5.ContainsKey(key) == false)
                DicFileMD5.Add(key, strMD5);
            else
                Debug.LogWarning("<Two File has the same name> name = " + filePath);
        }

        string savePath = System.IO.Path.Combine(Application.dataPath, "AssetBundlePackage/AssetBundle/") + platform + "/VersionNum";
        if (Directory.Exists(savePath) == false)
            Directory.CreateDirectory(savePath);

        // 删除前一版的old数据
        if (File.Exists(savePath + "/VersionMD5-old.xml"))
        {
            System.IO.File.Delete(savePath + "/VersionMD5-old.xml");
        }

        // 如果之前的版本存在，则将其名字改为VersionMD5-old.xml
        if (File.Exists(savePath + "/VersionMD5.xml"))
        {
            System.IO.File.Move(savePath + "/VersionMD5.xml", savePath + "/VersionMD5-old.xml");
        }

        FileCollection fc = new FileCollection();
        FileInfoMation fileinfo = null;
        List<FileInfoMation> fileinfolist = new List<FileInfoMation>();
        foreach (KeyValuePair<string, string> pair in DicFileMD5)
        {
            //去掉后缀名,因为Resources.Load不需要后缀名
            var path = pair.Key.Substring(0, pair.Key.LastIndexOf('.'));
            fileinfo = new FileInfoMation { Path = path, Hash = pair.Value };
            fileinfolist.Add(fileinfo);
        }
        fc.Files = fileinfolist.ToArray();
        XmlHelper.XmlSerializeToFile(fc, savePath + "/VersionMD5.xml", System.Text.Encoding.UTF8);
    }

    static Dictionary<string, string> ReadMD5File(string fileName)
    {
        Dictionary<string, string> DicMD5 = new Dictionary<string, string>();

        // 如果文件不存在，则直接返回
        if (System.IO.File.Exists(fileName) == false)
            return DicMD5;

        XmlDocument XmlDoc = new XmlDocument();
        XmlDoc.Load(fileName);
        XmlElement XmlRoot = XmlDoc.DocumentElement;

        foreach (XmlNode node in XmlRoot.ChildNodes)
        {
            if ((node is XmlElement) == false)
                continue;

            string file = (node as XmlElement).GetAttribute("FileName");
            string md5 = (node as XmlElement).GetAttribute("MD5");

            if (DicMD5.ContainsKey(file) == false)
            {
                DicMD5.Add(file, md5);
            }
        }

        XmlRoot = null;
        XmlDoc = null;

        return DicMD5;
    }

}